home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / serus221.zip / SERDEMO.C < prev    next >
Text File  |  1990-09-01  |  7KB  |  226 lines

  1.  
  2.    /********************************************************************/
  3.    /*                                                                  */
  4.    /*  Demonstration program for  SERIOUS  version 2.21                */
  5.    /*  by Norman J. Goldstein                                          */
  6.    /*                                                                  */
  7.    /********************************************************************/
  8.  
  9. /* This program illustrates how to program with  SERIOUS  by implementing
  10.    a simple terminal.  The file  serface.c  must be compiled with the
  11.    command line version of Turbo C, as it contains inline code.       */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <conio.h>
  16. #include "serface.h"
  17.  
  18. /*- - - - - - - - Ports and IRQ's - - - - - - - - - - - - - - - - - - - -
  19.   Here are some standard hardware configurations.
  20.  
  21.   DOS name    Port number       IRQ
  22.     COM1         3f8             4
  23.     COM2         2f8             3
  24.     COM3         3e8             4
  25.     COM4         2e8             3
  26.  
  27.   The DOS name is not important for using SERIOUS .  If these options do
  28.   not work in a particular system, the hardware should be checked/adjusted
  29.   for the desired settings.  The SERIOUS driver is designed to be the only
  30.   hardware handler for any one IRQ -- the driver does not chain to any
  31.   previously installed handlers.  This was a design decision for the
  32.   sake of security.  I do beleive that, under SERIOUS, different serial
  33.   ports can share the same IRQ .  Please contact me if this is a configuraion
  34.   that you would like to have.
  35.   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36.  
  37.  
  38. /*- - - - - - -  DispInBuff - - - - - - - - - - - - - -
  39.  * Transfers the input buffer to the screen.
  40.  *- - - - - - - - - - - - - - - - - - - - - - - - - -
  41.  */
  42. void DispInBuff(void)
  43. {
  44.  int ci;
  45.  
  46.  while( (ci = S_RecvChar()) != -1 ) putchar( ci );
  47. }/* DispInbuff */
  48.  
  49.  
  50. /* These are auxilliary bytes used in the routine  comm . */
  51. #define AltQ  16
  52. #define Alt0 129
  53.  
  54. /*- - - - -  comm - - - - - - - - - - - - - - - - - - - - - - -
  55.  * This is the dumb terminal
  56.  * Input: vptr -- This is the routine to display the input buffer.
  57.  *                In this file, it is called with  vptr = DispInBuff .
  58.  *- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  59.  */
  60. void comm( void (*vptr)(void) )
  61. {
  62.  int ci;       /* To hold characters */
  63.  int stay = 1; /* A flag to control the main loop */
  64.  char *cp;
  65.  
  66.  putchar('\n');    /* Set up the screen */
  67.  fflush( stdout );
  68.  
  69.  while(stay)  /* main loop ----------*/
  70.  {
  71.   if( kbhit() )
  72.   {
  73.    if( (ci = getch()) != 0 )
  74.                       {
  75.                        if( S_SendChar(ci) == ERR_UARTsleeps )
  76.                        /* This message bears bad tidings. */
  77.                        printf("\n!!! UART sleeps !!!\n");
  78.                       }
  79.  
  80.    else /* ci == 0  indicates that a special character was entered. */
  81.    switch( ci = getch() )
  82.    {
  83.     case AltQ:  stay = 0;          /* to leave  comm  */
  84.                 break;
  85.  
  86.     case Alt0:  S_SendChar('\0');  /* to send the null character */
  87.                 break;
  88.  
  89.     default:    /* Throw away other special characters. */
  90.                 break;
  91.    }/*switch*/
  92.   }/* kbhit */
  93.  
  94.   /* Display the input buffer. */
  95.   (*vptr)();
  96.  }/* while              --------------*/
  97. } /* comm */
  98.  
  99.  
  100. /*- - - - - - ChangeMode - - - - - - - - - -
  101.  * Set the interrupt mask for the UART .
  102.  * The mask bits are 0 -- Received data.
  103.  *                   1 -- Transmitter ready.
  104.  *                   2 -- Line error.
  105.  *                   3 -- Modem status change.
  106.  *- - - - - - - - - - - - - - - - - - - - -
  107.  */
  108. void ChangeMode(void)
  109. {
  110.  unsigned mask;
  111.  
  112.  printf("\nEnter interrupt mask MLTR: ");
  113.  scanf("%x" , &mask );
  114.  S_SetMode( (char) mask );
  115. }/*changeMode*/
  116.  
  117.  
  118. /*- - - - - -  param  - - - - - - - - - - - - - -
  119.  * Allows the transmission parameters within the UART to be set.
  120.  *- - - - - - - - - - - - - - - - - - - - - - - -
  121.  */
  122. void param(void)
  123. {
  124.  unsigned baud , u;
  125.  int data , stop , parity;
  126.  
  127.  printf("\nEnter baud , data , stop , parity(0:none 1:odd 3:even):\n");
  128.  scanf("%u %d %d %d", &baud, &data, &stop, &parity);
  129.  
  130.  if( (u=S_SetParms(baud , data , stop , parity)) == 0 )
  131.  printf("\nbaud  data  stop  parity= %u %d %d %d\n", baud, data, stop, parity);
  132.  else printf("Parameters not set. S_SetParms= %x .\n", u);
  133. }/* param */
  134.  
  135.  
  136. /*- - - - - - - menu - - - - - - - - -
  137.  * The main menu.  The options are:
  138.  * c -- Become a dumb terminal.
  139.  * p -- Set the transmission paramaters.
  140.  * m -- Set the interrupt mask.
  141.  * o -- Exit this program without closing SERIOUS .
  142.  *      WARNING: Be sure to be using the SERIOUS internal buffer, as this
  143.  *               programs's user supplied buffer is deallocated at exit!
  144.  * q -- Close SERIOUS and exit this program.
  145.  *- - - - - - - - - - - - - - - - - -
  146.  */
  147. static void menu(void)
  148. {
  149.  char c;
  150.  
  151.  do
  152.  {
  153.   printf("\nEnter <p>arameters, <m>ode ,  <c>omm , <q>uit , <o>s: ");
  154.   fflush( stdout );
  155.  
  156.   switch(  c = getche() )
  157.   {
  158.    case 'c':
  159.              comm( DispInBuff );
  160.              break;
  161.  
  162.    case 'm':
  163.              ChangeMode();
  164.              break;
  165.  
  166.    case 'o':
  167.              printf("\nSerial port active!\n");
  168.              exit(0);
  169.              break;
  170.    case 'p':
  171.              param();
  172.              break;
  173.  
  174.   }/* switch*/
  175.  } while( c != 'q' );
  176. } /*menu*/
  177.  
  178. /*- - - The user supplied buffer. - - - - - - -
  179.   The order is at most  16 . */
  180. #define InBuffOrd 10
  181. #define InBuffLength (1L << (InBuffOrd))
  182. static char InBuff[ InBuffLength ];
  183.  
  184. int main(int argc , char **argv)
  185. {
  186.  unsigned u , ord = InBuffOrd , port = 1,  /* Initially not 0 . */
  187.                                 irq;
  188.  
  189.  if( argc < 2 )
  190.  {
  191.   printf("\nUsage: sertest <device-name> [char]");
  192.   printf("\n       If  char  is omitted, use the user supplied buffer.");
  193.   printf("\n       char = '!' -- Assumes SERIOUS is already open.");
  194.   printf("\n       char = anything else -- Use internal SERIOUS buffer.");
  195.   exit(1);
  196.  }
  197.  
  198.  /* Check for a 2nd parameter. */
  199.  if( argc >= 3 )
  200.  {
  201.   if( *argv[2] == '!' ) port = 0;
  202.   else ord = 0;
  203.  }
  204.  
  205.  if(port) /* Driver is not yet opened. */
  206.  {
  207.   printf("\nEnter hex  port  and  irq: ");
  208.   scanf("%x%x", &port , &irq );
  209.  }
  210.  
  211.  
  212.  /* port == 0 : Assumes driver is open, just sets the entry address.
  213.     ord == 0 : Specify driver's default buffer to be used.
  214.   */
  215.  
  216.    u = S_Open( argv[1] , port , irq , InBuff , ord );
  217.    printf("\n S_Open= %X\n", u );
  218.    if(u) exit(0);
  219.  
  220.  menu();
  221.  
  222.  printf("\n S_Close= %X\n", S_Close() );
  223.  
  224.  return 0;
  225. }/*main*/
  226.